package shapewar.net.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import shapewar.a.widgets.Version;
import shapewar.activities.MainActivity;
import shapewar.conf.Conf;
import shapewar.engine.calc.Pos;
import shapewar.engine.intefac.IHittable;
import shapewar.engine.intefac.IMovable;
import shapewar.engine.intefac.ITank;
import shapewar.engine.presenter.Presenter;
import shapewar.net.io.OIS;
import shapewar.net.io.OOS;
import shapewar.net.seriz.CreateInfo;
import shapewar.net.seriz.DieInfo;
import shapewar.net.seriz.PosInfo;
import shapewar.net.seriz.Info;
import shapewar.net.seriz.MoveInfo;
import shapewar.net.seriz.NetConf;
import shapewar.net.seriz.ReliveInfo;
import shapewar.net.seriz.TextInfo;

public class Sender {
	OIS ois;
	OOS oos;
	Presenter pst;
	public Client client;
	public Sender(Presenter pst) {
		this.pst = pst;
	}

	public void setStream(OIS ois, OOS oos) {
		this.ois = ois;
		this.oos = oos;
	}
	
	public void send(Info info) {
		try {
			oos.writeObject(info);
			oos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void reqCreate(String name){
		CreateInfo ci = new CreateInfo();
		ci.cmd = NetConf.info_create;
		ci.name = name;
		ci.protector_num = Conf.protector_init_count;
		ci.map_width = Conf.px2dip(Conf.map_width);
		ci.map_height = Conf.px2dip(Conf.map_height);
		ci.speed = Conf.px2dip(Conf.tank_speed);
		ci.state = NetConf.state_alive;
		ci.mac = MainActivity.mac;
		send(ci);
	}
	
	public void reqMove(IMovable tank,Pos pos,int blt_key){
		MoveInfo mi = new MoveInfo();
		mi.id = tank.getId();
		mi.cmd = NetConf.info_move;
		mi.target_x = Conf.px2dip(pos.x);
		mi.target_y = Conf.px2dip(pos.y);
		mi.blt_key = blt_key;
		mi.x = Conf.px2dip(tank.getPos().x);
		mi.y = Conf.px2dip(tank.getPos().y);
		send(mi);
		Version.rememberTime1();
	}
	
	public void reqFire(IMovable tank,int blt_key){
		MoveInfo mi = new MoveInfo();
		mi.id = tank.getId();
		mi.cmd = NetConf.info_fire;
		mi.blt_key = blt_key;
		send(mi);
		Version.rememberTime1();
	}
	
	public void reqFlash(ITank tank,float tox,float toy){
		PosInfo fi = new PosInfo();
		fi.cmd = NetConf.info_flash;
		fi.id = tank.getId();
		fi.x = Conf.px2dip(tox);
		fi.y = Conf.px2dip(toy);
		send(fi);
	}
	
	public void reqProtectorDie(IHittable killed,IHittable killer,int blt_key){
		DieInfo di = new DieInfo();
		di.cmd = NetConf.info_protectorMinus;
		di.id = killed.getId();
		di.killerId = killer.getId();
		di.bulletKey = blt_key;
		send(di);
	}
	
	public void reqTankDie(IHittable killed,IHittable killer,int blt_key,int ptrNum){
		DieInfo di = new DieInfo();
		di.cmd = NetConf.info_die;
		di.id = killed.getId();
		di.killerId = killer.getId();
		di.bulletKey = blt_key;
		di.ptrNum = ptrNum;
		send(di);
	}
	 
	public void reqRelive(){
		ReliveInfo ri = new ReliveInfo();
		ri.cmd = NetConf.info_relive;
		ri.id = client.id;
		send(ri);
	}

	public void reqToast(String string) {
		TextInfo ti = new TextInfo();
		ti.cmd = NetConf.info_toast;
		ti.id = client.id;
		ti.str = string;
		send(ti);
	}
	
//	public void reqPosReset(){
//		PosInfo pi = new PosInfo();
//		pi.cmd = NetConf.info_posReset;
//		pi.id = client.id;
//		send(pi);
//	}
//	
}
